//
//  WHDefaultNotificationCenter.m
//  WHAnimations
//
//  Created by she1323 on 2017/3/21.
//  Copyright © 2017年 she1323. All rights reserved.
//  通知控制器





#import "WHDefaultNotificationCenter.h"

@interface WHDefaultNotificationModel : NSObject

@property (nonatomic, copy) NSString *name;

/**
 是否有效
 */
@property (nonatomic, assign) BOOL effective;

@end

@implementation WHDefaultNotificationModel
// 必须写，否则报错
@end


@interface WHDefaultNotificationCenter ()

/**
 通知数组。。。用通知数组保存起来可以不让同样的通知监听多次，没有的通知进行多余的删除
 */
@property (nonatomic, strong) NSMutableArray <WHDefaultNotificationModel *>*stringsArray;

@end

@implementation WHDefaultNotificationCenter


- (instancetype)init
{
    if (self = [super init]) {
        self.stringsArray = [NSMutableArray array];
    }
    return self;
}

#pragma mark 发送一个通知
+ (void)wh_postEventToNotificationName:(NSString *)name object:(id)object
{
    [[NSNotificationCenter defaultCenter] postNotificationName:name object:object];
}

#pragma mark 监听通知
- (void)wh_addNotificationName:(NSString *)name
{
    // 用于判断是否有相同的通知
    __block BOOL haveTheSameName = NO;
    
    [self.stringsArray enumerateObjectsUsingBlock:^(WHDefaultNotificationModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       // 如果传进来的通知名已经存在
        if ([obj.name isEqualToString:name]) {
            haveTheSameName = YES;
            // 结束此次循环
            *stop = YES;
        }
    }];
    
    // 如果该通知名原先不存在。添加一个通知
    if (haveTheSameName == NO) {
        WHDefaultNotificationModel *model = [[WHDefaultNotificationModel alloc] init];
        model.name = name;
        model.effective = YES;
        [self.stringsArray addObject:model];
        
        // 监听该通知
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationEvent:) name:model.name object:nil];
        
    }
}

#pragma mark 删除一个监听通知
- (void)wh_deleteNotificationName:(NSString *)name
{
    // 用于判断是否有相同的通知
    __block BOOL haveSameName = NO;
    // 用于记录找到通知
    __block NSInteger index = 0;
    [self.stringsArray enumerateObjectsUsingBlock:^(WHDefaultNotificationModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        // 有相同的通知
        if ([obj.name isEqualToString:name]) {
            haveSameName = YES;
            index = idx;
            *stop = YES;
        }
    }];
    
    // 如果存在要删除的通知就可以移除通知了，数组里的也要移除
    if (haveSameName == YES) {
        WHDefaultNotificationModel *model = self.stringsArray[index];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:model.name object:nil];
        [self.stringsArray removeObject:model];
    }
}

#pragma mark 得到所有的通知名
- (NSArray<NSString *> *)wh_notificationNames
{
    NSMutableArray *namesArray = [NSMutableArray array];
    [self.stringsArray enumerateObjectsUsingBlock:^(WHDefaultNotificationModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [namesArray addObject:obj.name];
    }];
    return namesArray;
}

#pragma mark 移除所有的通知
- (void)wh_removeAllNotifications
{
//    [self.stringsArray enumerateObjectsUsingBlock:^(WHDefaultNotificationModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        [[NSNotificationCenter defaultCenter] removeObserver:self name:obj.name object:nil];
//    }];

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    [self.stringsArray removeAllObjects];
}

- (void)notificationEvent:(NSNotification *)notification {
    // 监听到通知后
    if (self.delegate && [self.delegate respondsToSelector:@selector(defaultNotificationCenter:name:object:)]) {
        // 触发代理的通知回调事件
        [self.delegate defaultNotificationCenter:self name:notification.name object:notification.object];
    }
}

- (void)dealloc {
    
    [self wh_removeAllNotifications];
}

@end
